package com.lineage.server.model.Instance;

import com.lineage.server.model.L1GroundInventory;
import com.lineage.server.thread.NpcAiThreadPool;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NpcAI implements Runnable {
  private static final Log _log = LogFactory.getLog(NpcAI.class);
  
  private final L1NpcInstance _npc;
  
  public NpcAI(L1NpcInstance npc) {
    this._npc = npc;
  }
  
  public void startAI() {
    NpcAiThreadPool.get().execute(this);
  }
  
  public void run() {
    try {
      this._npc.setAiRunning(true);
      while (!this._npc._destroyed && !this._npc.isDead() && this._npc.getCurrentHp() > 0 && 
        this._npc.getHiddenStatus() == 0) {
        while (this._npc.isParalyzed() || this._npc.isSleeped()) {
          try {
            Thread.sleep(200L);
          } catch (InterruptedException e) {
            this._npc.setParalyzed(false);
          } 
        } 
        if (stopAIProcess())
          break; 
        try {
          Thread.sleep(this._npc.getSleepTime());
        } catch (Exception e) {
          break;
        } 
      } 
      this._npc.mobSkill().resetAllSkillUseCount();
      do {
        try {
          Thread.sleep(this._npc.getSleepTime());
        } catch (Exception e) {
          break;
        } 
      } while (this._npc.isDeathProcessing());
      this._npc.allTargetClear();
      this._npc.setAiRunning(false);
      Thread.sleep(20L);
    } catch (Exception e) {
      _log.error("NpcAI发生例外状况: " + this._npc.getName(), e);
    } 
  }
  
  private boolean stopAIProcess() {
    try {
      this._npc.setSleepTime(300);
      this._npc.checkTarget();
      if (this._npc.is_now_target() == null && this._npc.getMaster() == null)
        this._npc.searchTarget(); 
      this._npc.onItemUse();
      if (this._npc.is_now_target() == null) {
        this._npc.checkTargetItem();
        if (this._npc.isPickupItem() && 
          this._npc.is_now_targetItem() == null)
          this._npc.searchTargetItem(); 
        if (this._npc.is_now_targetItem() == null) {
          boolean noTarget = this._npc.noTarget();
          if (noTarget)
            return true; 
        } else {
          L1GroundInventory l1GroundInventory = 
            World.get().getInventory(
              this._npc.is_now_targetItem().getX(), 
              this._npc.is_now_targetItem().getY(), 
              this._npc.is_now_targetItem().getMapId());
          if (l1GroundInventory.checkItem(this._npc.is_now_targetItem().getItemId())) {
            this._npc.onTargetItem();
          } else {
            this._npc._targetItemList.remove(this._npc.is_now_targetItem());
            this._npc.set_now_targetItem((L1ItemInstance)null);
            this._npc.setSleepTime(1000);
            return false;
          } 
        } 
      } else if (this._npc.getHiddenStatus() == 0) {
        this._npc.onTarget();
      } else {
        return true;
      } 
      Thread.sleep(20L);
    } catch (Exception e) {
      _log.error("NpcAI发生例外状况: " + this._npc.getName(), e);
    } 
    return false;
  }
}
